package com.example.controller;

import com.example.common.Result;
import com.example.entity.ClubApplication;
import com.example.service.ClubApplicationService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/club/application")
public class ClubApplicationController {
    @Resource
    private ClubApplicationService clubApplicationService;

    /**
     * @method list 获取社团申请列表
     * @param clubId 社团ID
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return Result
     */
    @GetMapping("/list")
    public Result list(@RequestParam(required = false) Integer clubId,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        System.out.println("=== 开始处理获取社团申请列表请求 ===");
        System.out.println("请求参数: clubId=" + clubId + ", pageNum=" + pageNum + ", pageSize=" + pageSize);

        try {
            PageInfo<ClubApplication> pageInfo;
            if (clubId != null) {
                System.out.println("根据社团ID查询申请列表");
                pageInfo = clubApplicationService.getApplicationsByClubId(clubId, pageNum, pageSize);
            } else {
                System.out.println("查询所有社团申请列表");
                pageInfo = clubApplicationService.getAllApplications(pageNum, pageSize);
            }

            System.out.println("查询结果: 共" + pageInfo.getList().size() + "条记录");
            if (pageInfo.getList().size() > 0) {
                System.out.println("第一条记录: " + pageInfo.getList().get(0));
            }

            System.out.println("=== 请求处理完成 ===");
            return Result.success(pageInfo);
        } catch (Exception e) {
            System.out.println("处理请求时发生错误: " + e.getMessage());
            e.printStackTrace();
            return Result.error("500", "获取申请列表失败: " + e.getMessage());
        }
    }

    /**
     * @param application
     * @method 通过学生社团申请
     * @return Result
     */
    @PostMapping("/approve")
    public Result approve(@RequestBody ClubApplication application) {
        try {
            clubApplicationService.approveApplication(application.getId());
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }

    /**
     * @param application
     * @method reject 社团申请驳回
     * @return Result
     */
    @PostMapping("/reject")
    public Result reject(@RequestBody ClubApplication application) {
        try {
            clubApplicationService.rejectApplication(application.getId());
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }

    /**
     * @param clubId
     * @method getByClubId 根据社团ID查询社团申请列表
     * @return Result
     */
    @GetMapping("/getByClubId")
    public Result getByClubId(@RequestParam Integer clubId) {
        List<ClubApplication> clubmembers = clubApplicationService.getByClubId(clubId);
        return Result.success(clubmembers);
    }

    /**
     * @param param
     * @method getByCondition 根据条件查询社团申请列表
     * @return Result
     */
    @GetMapping
    public Result<PageInfo<ClubApplication>> getByCondition(ClubApplication param,
                                                            @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize
                                  ) {
        PageInfo<ClubApplication> clubApplicationPageInfo = clubApplicationService.getByCondition(param, pageNum, pageSize);

        return Result.success(clubApplicationPageInfo);
    }
} 